/*
 *    This file is part of BlobDB.
 *
 *    BlobDB is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    BlobDB is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with BlobDB.  If not, see <http://www.gnu.org/licenses/>.
 *    
 *    Copyright(c) 2011 Michael Kankkonen
 *    http://www.vnetcon.org
 */


package org.vnetcon.blobdb.database.backend.file.fdo;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;



public class Table {

    private String tableKey;
	private int tableID;
	private String schema;
	private String tableName;
	private byte[] columnInfo;
	private int rowsize;
	private int maxrows;

    private String datadir = null;
	
	public Table(int tableID, String schema, String tableName, int rowsize, int maxrows){
		this.schema = schema;
		this.tableName = tableName;
		this.rowsize = rowsize;
		this.maxrows = maxrows;
		this.tableID = tableID;
		InputStream fIn = this.getClass().getResourceAsStream("/blobdb/blobdb.properties");
		BufferedReader bfIn = new BufferedReader(new InputStreamReader(fIn));
		Properties p = new Properties();
		try{
			p.load(bfIn);
			this.datadir = p.getProperty("database.dir", null);
			bfIn.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
		this.init();
		
    	PMF.hTable.put(tableID, this);
	}

	private void init(){
    	try{
    		File f = new File(this.datadir + "/" + "TableProps_" + this.tableID + ".dat");
    		int iLength = -1;
    		byte[] byteBuf = null;
    		if(f.exists()){
    			FileInputStream fIn = new FileInputStream(f);
    			DataInputStream dfIn = new DataInputStream(fIn);
    			this.tableID = dfIn.readInt();
    			iLength = dfIn.readInt();
    			byteBuf = new byte[iLength];
    			dfIn.readFully(byteBuf);
    			this.schema = new String(byteBuf);
    			iLength = dfIn.readInt();
    			byteBuf = new byte[iLength];
    			dfIn.readFully(byteBuf);
    			this.tableName = new String(byteBuf);
    			this.rowsize = dfIn.readInt();
    			this.maxrows = dfIn.readInt();
    			fIn.close();
    			
    			this.columnInfo = this.getColumnInfo();
    			
    		}
    	}catch(Exception ex){
    		ex.printStackTrace();
    	}
	}
	
	public void setTableKey(String tableKey){
		this.tableKey = tableKey;
	}

	public String getSchema() {
		return schema;
	}


	public void setSchema(String schema) {
		this.schema = schema;
	}


	public String getTableName() {
		return tableName;
	}


	public void setTableName(String tableName) {
		this.tableName = tableName;
	}


	public byte[] getColumnInfo() {
    	try{
    		File f = new File(this.datadir + "/" + "TableCols_" + this.tableID + ".dat");
    		int iLength = -1;
    		byte[] bytebuf = null;
    		if(f.exists()){
    			iLength = (int)f.length();
    			bytebuf = new byte[iLength];
    			FileInputStream fIn = new FileInputStream(f);
    			fIn.read(bytebuf, 0, bytebuf.length);
    			fIn.close();
    			this.columnInfo = bytebuf;
    		}
    	}catch(Exception ex){
    		ex.printStackTrace();
    	}
		return columnInfo;
	}


	public void setColumnInfo(byte[] columnInfo) {
		this.columnInfo = columnInfo;
	}


	public String getTableKey() {
		return tableKey;
	}

	public int getRowsize() {
		return rowsize;
	}

	public int getMaxrows() {
		return maxrows;
	}

	public void setMaxrows(int maxrows) {
		this.maxrows = maxrows;
	}

	public int getTableID() {
		return tableID;
	}

	public void save(){
		File f = new File(this.datadir + "/" + "TableProps_" + this.tableID + ".dat");
		File f2 = new File(this.datadir + "/" + "TableCols_" + this.tableID + ".dat");
		byte[] byteBuf = null;
		FileOutputStream fOut = null;
		DataOutputStream dfOut = null;
		
		try{
			fOut = new FileOutputStream(f);
			dfOut = new DataOutputStream(fOut);
			dfOut.writeInt(this.tableID);
			byteBuf = this.schema.getBytes();
			dfOut.writeInt(byteBuf.length);
			dfOut.write(byteBuf, 0, byteBuf.length);
			byteBuf = this.tableName.getBytes();
			dfOut.writeInt(byteBuf.length);
			dfOut.write(byteBuf, 0, byteBuf.length);
			dfOut.writeInt(this.rowsize);
			dfOut.writeInt(this.maxrows);
			
			dfOut.flush();
			fOut.flush();
   			fOut.getFD().sync();
   			fOut.close();

			fOut = new FileOutputStream(f2);
			dfOut = new DataOutputStream(fOut);
			dfOut.write(this.columnInfo, 0, this.columnInfo.length);
			dfOut.flush();
			fOut.flush();
			fOut.getFD().sync();
			fOut.close();
   			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
	
}
